
sbit SDA=P1^7;
sbit SCL=P1^6;

void Delayus(unsigned int number)
{
 while(number--);
}

/*void DelayMs(unsigned int number)
{
 unsigned char temp;
 for(;number!=0;number--)
 {
  for(temp=112;temp!=0;temp--);
  {}
 }
}*/

void iic_start()
{
 SDA=1;
 Delayus(4);
 SCL=1;
 Delayus(4);
 SDA=0;
 Delayus(4);
 SCL=0;
 Delayus(4);
}

void iic_stop()
{
 SCL=0;
 Delayus(4);
 SDA=0;
 Delayus(4);
 SCL=1;
 Delayus(4);
 SDA=1;
 Delayus(4);
}

void iic_ack()
{
 SDA=0;
 Delayus(4);
 SCL=1;
 Delayus(4);
 SCL=0;
 Delayus(4);
 SDA=1;
 Delayus(4);
}

/*void iic_NoAck()
{
 SDA=1;
 Delayus(4);
 SCL=1;
 Delayus(4);
 SCL=0;
 Delayus(4);
 SDA=0;
}*/

bit iic_testack()
{
 bit ErrorBit;
 SDA=1;
 Delayus(4);
 SCL=1;
 Delayus(4);
 ErrorBit=SDA;
 Delayus(4);
 SCL=0;
 return ErrorBit;
}

void iic_write8bit(unsigned char input)
{
 unsigned char temp;
 for(temp=8;temp!=0;temp--)
 {
  SDA=(bit)(input&0x80);
  Delayus(4);
  SCL=1;
  Delayus(4);
  SCL=0;
  Delayus(4);
  input=input<<1;
 }
}

unsigned char iic_read8bit()
{
 unsigned char temp,rbyte=0;
 for(temp=8;temp!=0;temp--)
 {
  SCL=1;
  Delayus(4);
  rbyte=rbyte<<1;
  rbyte=rbyte|((unsigned char)SDA);
  SCL=0;
 }
 return rbyte; 
}
